<template>
	<view class="post-edit">
		<view class="form-item">
			<text class="label">标题</text>
			<input class="input" v-model="postInfo.title" placeholder="请输入帖子标题" />
		</view>
		
		<view class="form-item">
			<text class="label">内容</text>
			<textarea class="textarea" v-model="postInfo.content" placeholder="请输入帖子内容" />
		</view>
		
		<view class="form-item">
			<text class="label">图片</text>
			<view class="image-list">
				<view class="image-item" v-for="(item, index) in postInfo.images" :key="index">
					<image :src="item" mode="aspectFill" @click="previewImage(postInfo.images, index)"></image>
					<view class="delete-btn" @click="deleteImage(index)">
						<text class="iconfont icon-delete"></text>
					</view>
				</view>
				<view class="upload-btn" @click="chooseImage" v-if="postInfo.images.length < 9">
					<text class="iconfont icon-plus"></text>
				</view>
			</view>
		</view>
		
		<view class="submit-btn" @click="handleSubmit">保存</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			postId: '', // 帖子ID
			postInfo: {
				title: '',
				content: '',
				images: []
			}
		}
	},
	onLoad(options) {
		if (options.id) {
			this.postId = options.id
			this.loadPostDetail()
		}
	},
	methods: {
		// 加载帖子详情
		async loadPostDetail() {
			try {
				uni.showLoading({
					title: '加载中...'
				})
				
				const res = await uniCloud.callFunction({
					name: 'getPostDetail',
					data: {
						postId: this.postId
					}
				})
				
				if (res.result.code === 0) {
					const { title, content, images } = res.result.data
					this.postInfo = {
						title,
						content,
						images: images || []
					}
				} else {
					uni.showToast({
						title: res.result.msg || '加载失败',
						icon: 'none'
					})
				}
			} catch (error) {
				console.error('加载帖子详情失败', error)
				uni.showToast({
					title: '加载失败',
					icon: 'none'
				})
			} finally {
				uni.hideLoading()
			}
		},
		
		// 选择图片
		async chooseImage() {
			try {
				const res = await uni.chooseImage({
					count: 9 - this.postInfo.images.length,
					sizeType: ['compressed'],
					sourceType: ['album', 'camera']
				})
				
				// 上传图片
				for (let i = 0; i < res.tempFilePaths.length; i++) {
					const filePath = res.tempFilePaths[i]
					const cloudPath = `post/${Date.now()}-${Math.random().toString(36).substr(2)}.${filePath.split('.').pop()}`
					
					uni.showLoading({
						title: '上传中...'
					})
					
					const uploadRes = await uniCloud.uploadFile({
						filePath,
						cloudPath
					})
					
					this.postInfo.images.push(uploadRes.fileID)
				}
			} catch (error) {
				console.error('选择图片失败', error)
				uni.showToast({
					title: '选择图片失败',
					icon: 'none'
				})
			} finally {
				uni.hideLoading()
			}
		},
		
		// 删除图片
		deleteImage(index) {
			uni.showModal({
				title: '提示',
				content: '确定要删除这张图片吗？',
				success: (res) => {
					if (res.confirm) {
						this.postInfo.images.splice(index, 1)
					}
				}
			})
		},
		
		// 预览图片
		previewImage(images, current) {
			uni.previewImage({
				urls: images,
				current: current
			})
		},
		
		// 提交表单
		async handleSubmit() {
			// 表单验证
			if (!this.postInfo.title) {
				uni.showToast({
					title: '请输入帖子标题',
					icon: 'none'
				})
				return
			}
			
			if (!this.postInfo.content) {
				uni.showToast({
					title: '请输入帖子内容',
					icon: 'none'
				})
				return
			}
			
			try {
				uni.showLoading({
					title: '保存中...'
				})
				const userInfo = uni.getStorageSync('userInfo')
				const res = await uniCloud.callFunction({
					name: this.postId ? 'updatePost' : 'addPost',
					data: {
						postId: this.postId,
						userInfo: userInfo,
						...this.postInfo
					}
				})
				
				if (res.result.code === 0) {
					uni.showToast({
						title: '保存成功',
						icon: 'success'
					})
					
					// 返回上一页
					setTimeout(() => {
						uni.navigateBack()
					}, 1500)
				} else {
					uni.showToast({
						title: res.result.msg || '保存失败',
						icon: 'none'
					})
				}
			} catch (error) {
				console.error('保存帖子失败', error)
				uni.showToast({
					title: '保存失败',
					icon: 'none'
				})
			} finally {
				uni.hideLoading()
			}
		}
	}
}
</script>

<style>
.post-edit {
	min-height: 100vh;
	background-color: #f8f9fc;
	padding: 20rpx;
}

.form-item {
	margin-bottom: 30rpx;
}

.label {
	font-size: 28rpx;
	color: #333;
	margin-bottom: 10rpx;
	display: block;
}

.input {
	width: 100%;
	height: 80rpx;
	background-color: #fff;
	border-radius: 8rpx;
	padding: 0 20rpx;
	font-size: 28rpx;
}

.textarea {
	width: 100%;
	height: 300rpx;
	background-color: #fff;
	border-radius: 8rpx;
	padding: 20rpx;
	font-size: 28rpx;
}

.image-list {
	display: flex;
	flex-wrap: wrap;
	margin: 0 -10rpx;
}

.image-item {
	width: calc(33.33% - 20rpx);
	height: 200rpx;
	margin: 10rpx;
	position: relative;
}

.image-item image {
	width: 100%;
	height: 100%;
	border-radius: 8rpx;
}

.delete-btn {
	position: absolute;
	top: 10rpx;
	right: 10rpx;
	width: 40rpx;
	height: 40rpx;
	background-color: rgba(0, 0, 0, 0.5);
	border-radius: 50%;
	display: flex;
	align-items: center;
	justify-content: center;
}

.delete-btn .iconfont {
	color: #fff;
	font-size: 24rpx;
}

.upload-btn {
	width: calc(33.33% - 20rpx);
	height: 200rpx;
	margin: 10rpx;
	background-color: #f5f5f5;
	border-radius: 8rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}

.upload-btn .iconfont {
	color: #999;
	font-size: 60rpx;
}

.submit-btn {
	width: 100%;
	height: 80rpx;
	line-height: 80rpx;
	text-align: center;
	background-color: #4a6ee0;
	color: #fff;
	font-size: 32rpx;
	border-radius: 8rpx;
	margin-top: 40rpx;
}

/* 图标字体 */
@font-face {
	font-family: "iconfont";
	src: url('//at.alicdn.com/t/font_3456789_abcdef.woff2') format('woff2');
}

.iconfont {
	font-family: "iconfont" !important;
	font-style: normal;
	-webkit-font-smoothing: antialiased;
	-moz-osx-font-smoothing: grayscale;
}

.icon-delete:before {
	content: "\e67d";
}

.icon-plus:before {
	content: "\e767";
}
</style> 